package jetpack.com.pzl.architecture;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

import jetpack.com.pzl.R;

/**
 * 在 Fragment 之间共享数据
 */
public class FragmentMaster extends Fragment implements View.OnClickListener {
    //fragment间共享的数据
    private SharedViewModel model;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_fragment_master, container, false);
        //scope范围是同一个activity的周期内， ViewModelProviders.of(context)
        model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);

        Button b[] = new Button[3];
        b[0] = view.findViewById(R.id.btnViewModel1);
        b[1] = view.findViewById(R.id.btnViewModel2);
        b[2] = view.findViewById(R.id.btnViewModel3);
        for (Button btn : b) {
            btn.setOnClickListener(this);
        }

        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnViewModel1:
                ItemInfo itemInfo1 = new ItemInfo(1, "JJ", "Zui Chi Bi");
                model.selectSet(itemInfo1);
                break;
            case R.id.btnViewModel2:
                ItemInfo itemInfo2 = new ItemInfo(2, "Jay", "Shan Hu Hai");
                model.selectSet(itemInfo2);
                break;
            case R.id.btnViewModel3:
                ItemInfo itemInfo3 = new ItemInfo(3, "XueZhiQian", "Wind Likely");
                model.selectSet(itemInfo3);
                break;
        }
    }

}
